package com.newtype.meeting.client.mvc.frontend;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.View;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.store.TreeStore;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.newtype.meeting.client.AppEvents;
import com.newtype.meeting.client.model.MeetingCategory;
import com.newtype.meeting.client.model.MeetingRecord;
import com.newtype.meeting.client.model.MeetingReport;
import com.newtype.meeting.client.model.MeetingStart;
import com.newtype.meeting.client.mvc.AppView;
import com.newtype.meeting.client.widget.frontend.MeetingRecordWindow;
import com.newtype.meeting.client.widget.frontend.MeetingReportWindow;
import com.newtype.meeting.client.widget.frontend.StartMeetingPanel;
import com.newtype.meeting.client.widget.shared.OrganizationMultiSeperateSelectWindow;
import com.newtype.meeting.client.widget.shared.OrganizationSelectWindow;
import com.newtype.meeting.client.widget.shared.PersonnelSingleSelectWindow;
import com.newtype.meeting.shared.AccountSummaryDTO;
import com.newtype.meeting.shared.DepartmentTreeDTO;

public class StartMeetingView extends View {
	private LayoutContainer wrapper = Registry.get(AppView.CENTER_PANEL);
	private StartMeetingPanel startMeetingPanel;
	private MeetingReportWindow reportWindow;
	private MeetingRecordWindow recordWindow;
	private OrganizationSelectWindow organizationWindow;
	private PersonnelSingleSelectWindow personnelWindow;
	private OrganizationMultiSeperateSelectWindow organizationSeperateWindow;

	@Override
	protected void initialize() {
		super.initialize();
		personnelWindow = new PersonnelSingleSelectWindow();
	}

	public StartMeetingView(Controller controller) {
		super(controller);
	}

	@Override
	protected void handleEvent(AppEvent event) {
		if (event.getType().equals(AppEvents.ViewStartMeeting)) {
			onViewStartMeeting(event);
		} else if (event.getType().equals(AppEvents.ToReportWindow)) {
			onToReportWindow(event);
		} else if (event.getType().equals(AppEvents.ToRecordWindow)) {
			onToRecordWindow(event);
		} else if (event.getType().equals(AppEvents.ToOrganizationWindow)) {
			onToOrganizationWindow(event);
		} else if (event.getType().equals(AppEvents.ToPersonnelSingleWindow)) {
			onToPersonnelSingleWindow(event);
		} else if (event.getType().equals(AppEvents.ToStartMeeting)) {
			onToStartMeeting(event);
		} else if (event.getType().equals(
				AppEvents.ToOrganizationSeperateWindow)) {
			onToOrganizationSeperateWindow(event);
		}
	}

	private void onToPersonnelSingleWindow(AppEvent event) {
		DepartmentTreeDTO model = event.getData("model");
		TreeStore<ModelData> store = personnelWindow.getStore();
		store.removeAll();
		store.add(model.getChildren(), true);
		personnelWindow.show();
	}

	private void onToOrganizationWindow(AppEvent event) {
		DepartmentTreeDTO model = event.getData("model");
		Map<String, Field<String>> personnel = event.getData("personnel");
		organizationWindow = new OrganizationSelectWindow(personnel);
		TreeStore<ModelData> store = organizationWindow.getStore();
		store.removeAll();
		store.add(model.getChildren(), true);
		organizationWindow.show();
	}

	private void onToOrganizationSeperateWindow(AppEvent event) {
		DepartmentTreeDTO model = event.getData("model");
		Map<String, Field<String>> organization = event.getData("organization");
		organizationSeperateWindow = new OrganizationMultiSeperateSelectWindow(
				organization);
		TreeStore<ModelData> store = organizationSeperateWindow.getStore();
		store.removeAll();
		store.add(model.getChildren(), true);
		organizationSeperateWindow.show();
	}

	private void onToRecordWindow(AppEvent event) {
		MeetingRecord record = event.getData("record");
		recordWindow = new MeetingRecordWindow(record);
		recordWindow.show();
	}

	private void onToReportWindow(AppEvent event) {
		MeetingReport report = event.getData("report");
		reportWindow = new MeetingReportWindow(report);
		reportWindow.show();
	}

	private void onViewStartMeeting(AppEvent event) {
		wrapper.removeAll();
		//接收参数
		AccountSummaryDTO account = event.getData("account");
		List<MeetingCategory> cates = event.getData("cates");
		//实例化一个开始会议对象
		MeetingStart start = new MeetingStart();
		//初始化参数
		start.setDeptID(account.getDeptID());//部门ID
		start.setDeptName(account.getDeptName());//部门名称
		start.setOriginatorID(account.getAccountID());//发起人ID
		start.setOriginatorName(account.getFullName());//发起人姓名
		start.setChairID(account.getAccountID());//会议主席ID
		start.setChairName(account.getFullName());//主席姓名
		start.setRecorderID(account.getAccountID());//会议记录ID
		start.setRecorderName(account.getFullName());//记录姓名
		start.setStartDate(new Date());//开始日期
		start.setEndDate(new Date());//结束日期

		start.setStartTime("09:00");//开始时间
		start.setEndTime("18:00");//结束时间

		startMeetingPanel = new StartMeetingPanel(start, cates, null);
		ListStore<MeetingCategory> store = startMeetingPanel.getCates();
		store.removeAll();
		store.add(cates);
		wrapper.add(startMeetingPanel);
		wrapper.layout();
	}

	private void onToStartMeeting(AppEvent event) {
		wrapper.removeAll();
		//接收参数
		MeetingStart ms = event.getData("start");
		List<MeetingCategory> cates = event.getData("cates");
		MeetingCategory currentCate = event.getData("currentCate");
		startMeetingPanel = new StartMeetingPanel(ms, cates, currentCate);
		ListStore<MeetingCategory> store = startMeetingPanel.getCates();
		store.removeAll();
		store.add(cates);
		wrapper.add(startMeetingPanel);
		wrapper.layout();
	}

}
